﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Barbar.UrlGuard.Dal;

namespace Barbar.UrlGuard.Controls {
  public partial class AuthenticationWindowsIntegratedControl : UserControl, IInjector<AuthenticationWindowsIntegrated> {
    public AuthenticationWindowsIntegratedControl() {
      InitializeComponent();
    }

    private void SetFields() {
      txtUserName.Enabled = !cbxPassCurrentUserCredentials.Checked;
      lblUserName.Enabled = !cbxPassCurrentUserCredentials.Checked;
      txtPassword.Enabled = !cbxPassCurrentUserCredentials.Checked;
      lblPassword.Enabled = !cbxPassCurrentUserCredentials.Checked;
      cbxPassSpecificDomain.Enabled = !cbxPassCurrentUserCredentials.Checked;
      txtDomain.Enabled = !cbxPassCurrentUserCredentials.Checked && cbxPassSpecificDomain.Checked;
      lblDomain.Enabled = !cbxPassCurrentUserCredentials.Checked && cbxPassSpecificDomain.Checked;
    }

    private void cbxPassCurrentUserCredentials_CheckedChanged(object sender, EventArgs e) {
      SetFields();
    }

    private void cbxPassSpecificDomain_CheckedChanged(object sender, EventArgs e) {
      SetFields();
    }

    #region IReadOnlyInjector<AuthenticationWindowsIntegrated> Members

    public void LoadData(AuthenticationWindowsIntegrated readOnlyInstance) {
      if (readOnlyInstance == null) {
        cbxPassCurrentUserCredentials.Checked = true;
        cbxPassSpecificDomain.Checked = false;
        txtUserName.Text = string.Empty;
        txtPassword.Text = string.Empty;
        txtDomain.Text = string.Empty;
      } else {
        cbxPassCurrentUserCredentials.Checked = readOnlyInstance.UseCurrentUserCredentials;
        cbxPassSpecificDomain.Checked = readOnlyInstance.PassSpecifiedDomain;
        txtUserName.Text = readOnlyInstance.UserName;
        txtPassword.Text = readOnlyInstance.Password;
        txtDomain.Text = readOnlyInstance.Domain;
      }
    }

    #endregion

    #region IInjector<AuthenticationWindowsIntegrated> Members

    public void SaveData(AuthenticationWindowsIntegrated instance) {
      if (instance == null)
        throw new ArgumentNullException("instance");
      
      instance.UserName = txtUserName.Text;
      instance.Domain = txtDomain.Text;
      instance.Password = txtPassword.Text;
      instance.UseCurrentUserCredentials = cbxPassCurrentUserCredentials.Checked;
      instance.PassSpecifiedDomain = cbxPassSpecificDomain.Checked;
    }

    #endregion
  }
}
